package main

import (
	httpServer "github.com/asim/go-micro/plugins/server/http/v3"
	"github.com/asim/go-micro/v3"
	"main/domain/model"
	"net/http"

	"github.com/asim/go-micro/v3/registry"
	"github.com/asim/go-micro/v3/server"
	"github.com/gin-gonic/gin"
)

const (
	SERVER_NAME = "user-http"
	SERVER_ADDR = ":8080"
)

func main() {
	// 创建micro-http-server
	srv := httpServer.NewServer(
		server.Name(SERVER_NAME),
		server.Address(SERVER_ADDR),
	)

	// 注册gin路由
	gin.SetMode(gin.ReleaseMode)
	router := gin.New()
	router.Use(gin.Recovery())
	userHttp := &UserHttp{}
	userHttp.InitRouter(router)

	// 注册处理函数
	hd := srv.NewHandler(router)
	if err := srv.Handle(hd); err != nil {
		panic(err)
	}

	// 注册服务
	service := micro.NewService(
		micro.Server(srv),
		micro.Registry(registry.NewRegistry()),
	)
	service.Init()
	if err := service.Run(); err != nil {
		panic(err)
	}
}

type UserHttp struct {}

func (u *UserHttp) InitRouter(r *gin.Engine)  {
	r.POST("/info", u.Info)
}

func (u *UserHttp) Info(c *gin.Context)  {
	c.JSON(
		http.StatusOK,
		gin.H{
			"code": 0, "msg": "ok", "data": model.NewUser(),
		},
	)
}
